﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CTPAdapter;
using System.Threading;

namespace CTPDemo.Trader
{
    class Trader
    {

        CSTraderApi _traderApi;

        /// <summary>
        /// 交易API接口
        /// </summary>
        public CSTraderApi TraderApi
        {
          get { return _traderApi; }
          set { _traderApi = value; }
        }

        CSTraderSpiInter _CSTraderSpi;

        /// <summary>
        /// 回调函数Spi对象
        /// </summary>
        public CSTraderSpiInter CSTraderSpi
        {
            get { return _CSTraderSpi; }
            set { _CSTraderSpi = value; }
        }

        ///// <summary>
        ///// 生成交易类对象
        ///// </summary>
        ///// <returns>交易类对象</returns>
        //public static Trader GetInstance(string investorID, string password)
        //{
            
        //    ///没有对象的情况，生成对象
        //    if (_trader == null)
        //    {
        //        _trader = new Trader();
        //    }
        //    return _trader;
        //}

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="investorID"></param>
        /// <param name="password"></param>
        public Trader(string investorID, string password)
        {
            _traderApi = new CSTraderApi();
            _CSTraderSpi = new TraderSpi(investorID, password, _traderApi);
            _traderApi.RegisterSpi(_CSTraderSpi);
            _traderApi.SubscribePublicTopic(CSTHOST_TE_RESUME_TYPE.CSTHOST_TERT_QUICK);
            _traderApi.SubscribePrivateTopic(CSTHOST_TE_RESUME_TYPE.CSTHOST_TERT_QUICK);
            _traderApi.RegisterFront("tcp://asp-sim2-front1.financial-trading-platform.com:26205");
            _traderApi.Init();

        }
        public void Release()
        {
            Thread.Sleep(5000);
            ((TraderSpi)_CSTraderSpi).Release();
        }

    }
}
